package com.hmsg.health.controller.admin;


import com.hmsg.health.bean.Dto.AdminUserTaskDto;
import com.hmsg.health.bean.Dto.TaskDto;
import com.hmsg.health.bean.Dto.TaskTypeDto;
import com.hmsg.health.bean.domain.TaskDo;
import com.hmsg.health.bean.domain.TaskTypeDo;
import com.hmsg.health.bean.domain.UserDo;
import com.hmsg.health.bean.enums.StatusCode;
import com.hmsg.health.dao.TaskDao;
import com.hmsg.health.dao.TaskTypeDao;
import com.hmsg.health.dao.UserDao;
import com.hmsg.health.dao.UserTaskDao;
import com.hmsg.health.param.ResponseResult;
import com.hmsg.health.security.IgnoreSecurity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Api(value = "运营系统用户发布任务相关", tags = { "运营系统用户发布任务相关" })
@Slf4j
@Controller
@RequestMapping("/task")
public class AdminUserTaskController {
    @Autowired
    private TaskDao taskDao;

    @Autowired
    private UserTaskDao userTaskDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private TaskTypeDao taskTypeDao;

    /**
     *
     * @param typeId
     * @param state 0 正常  -1失效 2 已完成
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value="用户发布任务列表", notes="用户发布任务列表" ,httpMethod="GET")
    @ResponseBody
    @IgnoreSecurity
    public ResponseResult<AdminUserTaskDto> queryList(  @RequestParam(value = "typeId",required = false) Integer typeId,
                                                        @RequestParam(value = "state",required = false) Integer state,
                                                        @RequestParam(value = "phone",required = false) String phone,
                                                        @RequestParam(value = "time", required = false) String time,
                                                        @RequestParam(value = "pageNo") Integer pageNo,
                                                       @RequestParam(value = "pageSize") Integer pageSize) {

        Integer firstResult = (pageNo-1)*pageSize;
        Integer maxResult =pageSize;

        int totalpages;//总页数
        int total=taskDao.findCount(typeId,state,phone,time);
        totalpages=   total/pageSize;
        if(total% pageSize !=0){
            totalpages++;
        }

        AdminUserTaskDto adminUserTaskDto=new AdminUserTaskDto();

        List<TaskDto> taskList=taskDao.findAll(typeId,state,phone,time,firstResult,maxResult);
        if(taskList != null && taskList.size()>0){
            for(TaskDto ta:taskList){
                //异常人数
                Integer failCount=userTaskDao.findFailCount(ta.getId());
                if(failCount !=  null) {
                    ta.setFailCount(failCount);
                }
                //用户名
                UserDo select = userDao.select(ta.getUserId());
                if(select != null){
                    ta.setUserName(select.getUserName());
                    ta.setPhone(select.getMobile());
                }
                //已完成人数
                Integer finishCount=userTaskDao.finishCount(ta.getId());
                if(finishCount !=  null) {
                    ta.setFinishCount(finishCount);
                }
                TaskTypeDo one = taskTypeDao.findOne(ta.getType());
                if(one != null){
                    ta.setSlogan(one.getSlogan());
                    //任务的步骤一 步骤二
                    ta.setTepOne(one.getTepOne());
                    ta.setTepTwo(one.getTepTwo());
                }




            }
        }
        adminUserTaskDto.setTotalpages(totalpages);
        adminUserTaskDto.setTaskDtoList(taskList);

        return new ResponseResult<AdminUserTaskDto>(StatusCode.SYSTEM_SUCCESS, adminUserTaskDto);
    }

    @GetMapping("/classType")
    @ApiOperation(value="任务类型", notes="任务类型" ,httpMethod="GET")
    @ResponseBody
    @IgnoreSecurity
    public ResponseResult< List<TaskTypeDto>> queryList() {
        List<TaskTypeDo> all = taskTypeDao.findAll();
        List<TaskTypeDto> taskTypeDto1=new ArrayList<>();
        if(all != null && all.size()>0){

            for(TaskTypeDo ta:all){
                TaskTypeDto taskTypeDto=new TaskTypeDto();
                taskTypeDto.setId(ta.getId());
                taskTypeDto.setName(ta.getName());
                taskTypeDto1.add(taskTypeDto);
            }
            return new ResponseResult< List<TaskTypeDto>>(StatusCode.SYSTEM_SUCCESS, taskTypeDto1);
        }
        return new ResponseResult< List<TaskTypeDto>>(StatusCode.SYSTEM_SUCCESS, taskTypeDto1);
    }
}
